﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.Ajax.ASP
{
    public partial class rank_month_score : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int correct = int.Parse(Request.QueryString["correct"]);
            int result = int.Parse(Request.QueryString["result"]);
            int rank_id = int.Parse(Request.QueryString["id"]);
            int user_id = int.Parse(Request.QueryString["user_id"]);
            int test_id = int.Parse(Request.QueryString["test_id"]);
            if (user_id == 0)
            {
                Response.Redirect("~/trac-nghiem/ket-qua-" + result);
            }
            else
            {
                var get_role = new TblUserController().FetchAll().Where(x => x.UserId == user_id).Single();
                if (get_role.UserLevel == 5)
                {
                    Response.Redirect("~/trac-nghiem/ket-qua-" + result);
                }
                else
                {
                    var get_question = new TblTestDetailController().FetchAll().Where(x => x.TestId == test_id).ToList();

                    int score = 100 * correct / get_question.Count;


                    var get_rank = new TblRankMonthScoreController().FetchAll().Where(x => x.UserID == user_id && x.TestId == test_id && x.RankDate == rank_id).ToList();
                    if (get_rank.Count == 0)
                    {
                        new TblRankMonthScoreController().Insert(user_id, score, test_id, rank_id);
                    }
                    else
                    {
                        new TblRankMonthScoreController().Update(get_rank[0].Id, user_id, (get_rank[0].Score + score) / 2, test_id, rank_id);
                    }

                    Response.Redirect("~/trac-nghiem/ket-qua-" + result);
                }
            }

        }
    }
}